import { useAuthStore } from '@/store/modules/auth';
import { localStg } from '@/utils/storage';

export function useAuth() {
  const authStore = useAuthStore();

  function hasAuth(codes: string | string[]) {
    if (!authStore.isLogin) {
      return false;
    }

    // 判断是超级管理员角色，直接赋予全部权限
    const userInfo = localStg.get(`userInfo`);
    if (userInfo && userInfo.roles.includes(import.meta.env.VITE_STATIC_SUPER_ROLE)) {
      return true;
    }

    if (!codes) {
      return true;
    }

    if (typeof codes === 'string') {
      return authStore.userInfo.perms.includes(codes);
    }

    return codes.some(code => authStore.userInfo.perms.includes(code));
  }

  return {
    hasAuth
  };
}
